#include "gData.h"

void gData::init()
{
	m_playerRot = randF(0,360);
	m_playerRad = 20.0f;

	m_galxRad = GALX_RAD;
	m_quadAmt = QUAD_AMT;

	genValidPos();
}

void gData::release()
{
	m_quadRad.release();
	m_quadPos.release();

	for(int i = 0; i < m_quads.size(); i++)
	{
		m_quads[i]->release();
		delete m_quads[i];
		m_quads[i] = 0;
	}
	m_quads.release();
}

void gData::genValidPos()
{
	//generate valid quadrant position with spacing
	m_quadPos.clear();
	for(int i = 0; i < m_quadAmt; i++)
	{
		float ang = randF(0,360);
		float rad = randF(QUAD_MIN,QUAD_MAX)*2.0f;
		float dis = randF(0,m_galxRad-rad);
		V2DF  pos = V2DF(dis*cos(RADIAN(ang)),dis*sin(RADIAN(ang)));
		
		bool valid = true;
		for(int j = 0; j < m_quadPos.size(); j++)
		{
			if(C2COverlap(pos,rad,m_quadPos[j],m_quadRad[j]*2.0f))
			{
				valid = false;
				break;
			}
		}

		if(valid)
		{
			m_quadPos.insertBack(pos);
			m_quadRad.insertBack(rad/2.0f);
		}
		else
			i--;
	}

	//generate quadrants info
	for(int i = 0; i < m_quadAmt; i++)
	{
		gQuadrant* quad = new gQuadrant;
		quad->init(m_quadRad[i],m_playerRad);
		m_quads.insertBack(quad);
	}

	float ang = randF(0,360);
	float rad = randF(QUAD_MIN,QUAD_MAX)*2.0f;
	float dis = randF(0,m_galxRad-rad);
	V2DF  pos = V2DF(dis*cos(RADIAN(ang)),dis*sin(RADIAN(ang)));
	m_playerPos.set(pos.x,0,pos.y);
}